package one.coder.netty.chapter.fourteen;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import one.coder.netty.chapter.eight.ObjectClientHandler;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;

/**
 * @author lihzh
 * @alia OneCoder
 * @blog http://www.coderli.com
 */
public class OioClient {

	/**
	 * @param args
	 * @author lihzh
	 * @alia OneCoder
	 * @blog http://www.coderli.com
	 */
	public static void main(String[] args) {
		// Client服务启动器
		ClientBootstrap bootstrap = new ClientBootstrap(
				new OioClientSocketChannelFactory(
						Executors.newFixedThreadPool(1)));
		// 设置一个处理服务端消息和各种消息事件的类(Handler)
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			@Override
			public ChannelPipeline getPipeline() throws Exception {
				return Channels.pipeline(new ObjectEncoder(),
						new ObjectClientHandler());
			}
		});
		// 连接到本地的8000端口的服务端
		bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));
	}

}
